package com.itheima.project.knowledge;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima.project.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SubPageActivity extends AppCompatActivity {

    public static final String SUB_PAGE_TITLE = "sub_page_title";
    public static final String CHILDREN_BEAN = "children_bean";

    private List<KnowledgeTreeEntity.DataBean.ChildrenBean> mChildrenBeanList;

    private List<SubPageContentFragment> mVpList = new ArrayList<>();

    private TextView tvTitle;
    private RecyclerView rvNavigation;
    private ViewPager vpContent;
    private RvNavigationAdapter rvNavigationAdapter;
    private SubPageViewPageAdapter subPageViewPageAdapter;
    private ImageView ivReturn;
    private SubPageContentFragment subPageContentFragment;
    private int number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.knowlegde_sub_page_activity);
        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        ivReturn = findViewById(R.id.iv_return_sub_page_activity);
        tvTitle = findViewById(R.id.tv_title_sub_page_activity);
        rvNavigation = findViewById(R.id.rv_navigation_sub_page_activity);
        vpContent = findViewById(R.id.vp_content_sub_page_activity);
    }

    private void initData() {
        //标题部分
        Intent intent = getIntent();
        if (intent != null) {
            String name = intent.getStringExtra(SUB_PAGE_TITLE);
            mChildrenBeanList = (List<KnowledgeTreeEntity.DataBean.ChildrenBean>) intent.getSerializableExtra(CHILDREN_BEAN);
            tvTitle.setText(name);
        }
        //导航栏部分
        rvNavigation.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
        rvNavigationAdapter = new RvNavigationAdapter(mChildrenBeanList,rvNavigation);
        rvNavigation.setAdapter(rvNavigationAdapter);
        //正文部分
        subPageContentFragment = new SubPageContentFragment();
        int size = mChildrenBeanList.size();
        for(int i = 0 ; i < size ;i++){
            mVpList.add(subPageContentFragment.newInstance(mChildrenBeanList.get(i)));
        }
        subPageViewPageAdapter = new SubPageViewPageAdapter(getSupportFragmentManager(), mVpList);
        vpContent.setAdapter(subPageViewPageAdapter);


    }

    private void initEvent() {
        rvNavigationAdapter.setOnIndicatorItemClickListener(
                new RvNavigationAdapter.onIndicatorItemClickListener() {
                    @Override
                    public void onIndicatorItemClick(
                            int position,
                            KnowledgeTreeEntity.DataBean.ChildrenBean childrenBean) {
                        vpContent.setCurrentItem(position);
                    }
                });
        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                number = position;
                rvNavigationAdapter.setCurrentItem(position);

            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SubPageContentFragment contentFragment = mVpList.get(number);
                contentFragment.scrollToTop();
            }
        });
    }
    public static void startSubPageActivity(Context context,
                                            String title,
                                            List<KnowledgeTreeEntity.DataBean.ChildrenBean> treeList){
        Intent intent = new Intent(context,SubPageActivity.class);
        intent.putExtra(SUB_PAGE_TITLE,title);
        intent.putExtra(CHILDREN_BEAN,(Serializable)treeList);
        context.startActivity(intent);

    }
}
